﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ionic.Zip;

namespace OpenPluginInstaller
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                //Check for the plugin's name
                if (args[0].Substring(args[0].Length - 4, 4) == "olpk")
                {
                    //Okay, plugin verified, so now we extract it and copy it to the OpenLaunch Plugin directory.
                    try
                    {
                        //Decrypt the file first.
                        DarthEncrypt f = new DarthEncrypt();
                        f.PassPhrase = "AE47FAB9E3D390C4E7B2F1";
                        byte[] m = f.DecryptBytes(File.ReadAllBytes(args[0]));
                        Directory.CreateDirectory(Environment.CurrentDirectory + "\\data\\");
                        string unpackDirectory = Environment.CurrentDirectory + "\\data\\";
                        using (ZipFile zip1 = ZipFile.Read(m))
                        {
                            // here, we extract every entry, but we could extract conditionally
                            // based on entry name, size, date, checkbox status, etc.  
                            foreach (ZipEntry e in zip1)
                            {
                                e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
                            }
                        }
                        foreach (string g in Directory.GetFiles(Environment.CurrentDirectory + "\\data\\"))
                        {
                            //File.Move(g, Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\Styko Company\\OpenLaunch\\Plugins\\" + g.Substring(g.LastIndexOf('\\') + 1));
                        }
                        //File.Delete(args[0]);
                        //Directory.Delete(Environment.CurrentDirectory + "\\data\\");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            else
            {
                Console.WriteLine("No plugin specified");
            }
        }
    }
}
